package com.jar.netty_demo.Netty.http;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class HttpServer {
    private static final int PORT = 9999;

    private void run() throws InterruptedException {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128) // 设置线程队列得到连接个数
                .childOption(ChannelOption.SO_KEEPALIVE, true)//TCP会实现监控连接是否有效
                // .handler(null)  handler 和 childHandler 前者是在bossGroup的handler,后者是workGroup
                .childHandler(new HttpChannelInitializer());

        ChannelFuture future = bootstrap.bind(PORT).sync();
        future.channel().closeFuture().sync();

    }
    public static void main(String[] args) throws InterruptedException {
        new HttpServer().run();
    }
}
